// pages/find_owner_pet/find_owner_pet.js
var Url = "http://localhost:8085/";
var call = require("../../utils/ajax.js")
var petSelectList = require("../../utils/petSelectList.js")
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        border: "",
        color: "",
        URL: "",
        //回到顶部
        floorstatus: false,
        //判断是否改变值
        states: "",
        show: false, //控制下拉列表的显示隐藏，false隐藏、true显示
        selectData: ['地区', '福州', '龙岩', '南平', '宁德', '莆田', '泉州', '莆田', '泉州', '三明', '厦门', '漳州'], //下拉列表的数据
        index: 0,
        hideModal: true,
        sb: false,

        currentData: 0,
        // ab:[],
        petList: [],
        //当前选中数组的下标值
        customIndex: [0, 0, 0],
        //当前选中数组
        onlyArray: [
            [],
            [],
            []
        ],
        //customArray假设为我们从后台获取到的json数据
        customArray: [],
        tokenID: "",
        area: "",
        loadingMore: true,
        noMore: false,
        loadingFailed: false,
        loading: false,
        //初始页
        pageNo: 1,

    },
    selectTap() {
        this.setData({
            show: !this.data.show
        });
    },
    optionTap(e) {
        console.log(e.currentTarget.dataset.city);
        let Index = e.currentTarget.dataset.index; //获取点击的下拉列表的下标
        let that = this;
        this.setData({
            index: Index,
            show: !this.data.show,
            city: e.currentTarget.dataset.city
        });
        this.adoptList();
        this.goTop();
    },
    changeTab: function(e) {
        console.log(e)
        const that = this;
        console.log(e.currentTarget.dataset.current);
        that.setData({
            currentData: e.currentTarget.dataset.current
        })
    },

    adoptList: function() {
        let that = this;
        let category = this.data.pet_category;
        let city = this.data.city;
        let sex = this.data.pet_sex;
        let varieties = this.data.pet_varieties;
        let url = "Pet/selectManyPage?pageNum=1&pageSize=4&";
        if (category != undefined && category != "") {
            url = url + "pet_category=" + category + "&"
        }
        if (city != undefined && city != "" && city != "地区") {
            url = url + "city_area=" + city + "&"
        }
        if (sex != undefined && sex != "") {
            url = url + "pet_sex=" + sex + "&"
        }
        if (varieties != undefined && varieties != "") {
            url = url + "pet_varieties=" + varieties + "&"
        }
        console.log(url)
        call.getData1(url, function(res) {
                console.log(res)
                let jj = res.data.list;
                console.log(jj)
                that.setData({
                    petList: jj,
                    loading: false,
                    noMore: false,
                    loadingFailed: false,
                    loadingMore: true,
                    pageNo: 1
                })

                // for (var c = 0; c < jj.length; c++) {
                //     if (jj[c].pet_sterilization === "已绝育") {
                //         that.setData({
                //             border: "1rpx solid #4B4B4B",
                //             color: "#4B4B4B"
                //         })
                //     }
                // switch (jj[c].pet_sterilization) {
                //     case "未绝育":
                //         that.setData({
                //             border: "1rpx solid #4B4B4B",
                //             color: "#4B4B4B"
                //         })
                //         return
                //     case "已绝育":
                //         that.setData({
                //             border: "1rpx solid rgb(210, 44, 31)",
                //             color: " rgb(210, 44, 31)"
                //         })
                //         return
                // }
                // console.log(jj[c].pet_sterilization)
                // }
            },
            function(e) {
                console.log(e)
            })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            URL: app.globalData.URL
        })
        let List = petSelectList.petList();
        this.setData({
            customArray: List
        })
        console.log(List)
        console.log(this.data.customArray)
        let jj = this.lal();
        console.log(jj)
        let kk = this.lal().user_id;
        console.log(kk)
        if (kk != undefined) {
            this.setData({
                sb: false
            })
        } else {
            console.log("admin")
        }
        var data = {
            customArray: this.data.customArray,
            customIndex: this.data.customIndex,
            onlyArray: this.data.onlyArray,
        };
        for (var i = 0; i < data.customArray.length; i++) {
            data.onlyArray[0].push(data.customArray[i].name);
        }
        for (var j = 0; j < data.customArray[data.customIndex[0]].dept.length; j++) {
            data.onlyArray[1].push(data.customArray[data.customIndex[0]].dept[j].name);
        }
        for (var k = 0; k < data.customArray[data.customIndex[0]].dept[data.customIndex[1]].product.length; k++) {
            data.onlyArray[2].push(data.customArray[data.customIndex[0]].dept[data.customIndex[1]].product[k].name);
        }
        this.setData(data);
        console.log(data)

        let token = wx.getStorageSync('tokenID');
        this.setData({
            tokenID: token
        })
        this.adoptList();
    },
    lal: function() {
        let make = "";
        let kk = this;
        console.log("s")
        make = wx.getStorageSync('mm')
        return make;
    },

    lal: function() {
        let make = "";
        let kk = this;
        console.log("s")
        make = wx.getStorageSync('mm')
        return make;
    },

    // 获取滚动条当前位置
    onPageScroll: function(e) {
        // console.log(e)
        if (e.scrollTop > 3) {
            this.setData({
                floorstatus: true
            });
        } else {
            this.setData({
                floorstatus: false
            });
        }
    },
    //回到顶部
    goTop: function(e) { // 一键回到顶部
        if (wx.pageScrollTo) {
            wx.pageScrollTo({
                scrollTop: 0
            })
        } else {
            wx.showModal({
                title: '提示',
                content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
            })
        }
    },

    // 获取滚动条当前位置
    onPageScroll: function(e) {
        // console.log(e)
        if (e.scrollTop > 3) {
            this.setData({
                floorstatus: true
            });
        } else {
            this.setData({
                floorstatus: false
            });
        }
    },
    //回到顶部
    goTop: function(e) { // 一键回到顶部
        if (wx.pageScrollTo) {
            wx.pageScrollTo({
                scrollTop: 0
            })
        } else {
            wx.showModal({
                title: '提示',
                content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
            })
        }
    },


    bindCustomPickerChange: function(e) {
        var that = this;
        var customArray = this.data.customArray,
            customIndex = this.data.customIndex,
            onlyArray = this.data.onlyArray;
        console.log(onlyArray[0] + customIndex)
        console.log('picker发送选择改变，携带值为', e.detail.value);
        //此处e.detail.value为当前选择的列的下标值数组，如[0,1,0]

        console.log('picker最终选择值为：', onlyArray[0][customIndex[0]], onlyArray[1][customIndex[1]], onlyArray[2][customIndex[2]]);
        this.setData({
            customIndex: e.detail.value
        })
        let pet_category = "";
        let pet_sex = "";
        let pet_varieties = "";
        if (onlyArray[0][customIndex[0]] != "类别") {
            pet_category = onlyArray[0][customIndex[0]];
        }
        if (onlyArray[1][customIndex[1]] != "性别") {
            pet_sex = onlyArray[1][customIndex[1]];
        }
        if (onlyArray[2][customIndex[2]] != "品种") {
            pet_varieties = onlyArray[2][customIndex[2]];
        }
        that.setData({
            pet_category: pet_category,
            pet_sex: pet_sex,
            pet_varieties: pet_varieties,
            states: "执行了"
        })
        let state = "";
        state = that.data.states;
        if (state == "执行了") {
            that.adoptList()
        } else {
            that.adoptList()
        }
        this.goTop();

    },

    //多列自创选择器换列方法
    bindCustomPickerColumnChange: function(e) {
        var customArray = this.data.customArray,
            customIndex = this.data.customIndex,
            onlyArray = this.data.onlyArray;

        customIndex[e.detail.column] = e.detail.value;
        // console.log(onlyArray);

        var searchColumn = () => {
            for (var i = 0; i < customArray.length; i++) {
                var arr1 = [];
                var arr2 = [];
                if (i == customIndex[0]) {
                    for (var j = 0; j < customArray[i].dept.length; j++) {
                        arr1.push(customArray[i].dept[j].name);
                        if (j == customIndex[1]) {
                            for (var k = 0; k < customArray[i].dept[j].product.length; k++) {
                                arr2.push(customArray[i].dept[j].product[k].name);
                            }
                            onlyArray[2] = arr2;
                        }
                    }
                    onlyArray[1] = arr1;
                }
            };
        }

        switch (e.detail.column) {
            case 0:
                customIndex[1] = 0;
                customIndex[2] = 0;
                searchColumn();
                break;
            case 1:
                customIndex[2] = 0;
                searchColumn();
                break;
        }
        this.setData({
            onlyArray: onlyArray,
            customIndex: customIndex
        });
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        // this.goTop();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        let admin = this;
        console.log("拉到位");
        console.log("寻主")
        if (!this.data.loading && !this.data.noMore) {
            admin.setData({
                loading: true,
                pageNo: this.data.pageNo + 1
            });
            admin.getData(true);
        }
    },
    //请求数据
    getData(isPage) {
        let admin = this;
        // console.log("需要拼接数据：", Network.pageData);
        let params = {
            pageNum: admin.data.pageNo,
            pageSize: 4
        };
        //请求
        admin.setData({
            loadingMore: false
        });
        let that = this;
        let category = this.data.pet_category;
        let city = this.data.city;
        let sex = this.data.pet_sex;
        let varieties = this.data.pet_varieties;
        let url = "Pet/selectManyPage?pageNum=" + params.pageNum + "&pageSize=" + params.pageSize + "&";
        if (category != undefined && category != "") {
            url = url + "pet_category=" + category + "&"
        }
        if (city != undefined && city != "" && city != "地区") {
            url = url + "city_area=" + city + "&"
        }
        if (sex != undefined && sex != "") {
            url = url + "pet_sex=" + sex + "&"
        }
        if (varieties != undefined && varieties != "") {
            url = url + "pet_varieties=" + varieties + "&"
        }
        console.log(url)
        call.getData1(url, function(res) {
            console.log(res)
            let jj = res.data.list;
            console.log(jj);
            console.log(res.data.pageNum)
            admin.setData({
                loading: false,
                loadingMore: false
            })
            if (res.val == 200) {
                if (isPage && res.data.pageNum != 1) {
                    //下一页的数据拼接在原有数据后面
                    if (res.data.pageNum != 1 && admin.data.pageNo <= res.data.pages) {
                        admin.setData({
                            petList: admin.data.petList.concat(jj)
                        })
                    }

                } else {
                    //第一页数据直接赋值
                    admin.setData({
                        petList: jj
                    })
                }
                //如果返回的数据为空，那么就没有下一页了
                if (res.data.list.length == 0) {
                    admin.setData({
                        noMore: true
                    })
                }
                //如果是最后一页，那么就没有下一页了
                if (res.data.pageNum == res.data.pages) {
                    admin.setData({
                        noMore: true
                    })
                }
            } else {
                //返回失败
                admin.setData({
                    loadingFailed: true
                })
            }
        }, function(e) {
            console.log(e)
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    // 显示遮罩层
    showModal: function() {
        var that = this;
        that.setData({
            hideModal: false
        })

        var animation = wx.createAnimation({
            duration: 100, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
            timingFunction: 'ease', //动画的效果 默认值是linear
        })
        this.animation = animation
        setTimeout(function() {
            that.fadeIn(); //调用显示动画
        }, 100)
    },

    // 隐藏遮罩层
    hideModal: function() {
        var that = this;
        var animation = wx.createAnimation({
            duration: 100, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
            timingFunction: 'ease', //动画的效果 默认值是linear
        })
        this.animation = animation
        that.fadeDown(); //调用隐藏动画   
        setTimeout(function() {
                that.setData({
                    hideModal: true
                })
            }, 100) //先执行下滑动画，再隐藏模块

    },

    //动画集
    fadeIn: function() {
        this.animation.translateY(0).step()
        this.setData({
            animationData: this.animation.export() //动画实例的export方法导出动画数据传递给组件的animation属性
        })
    },
    fadeDown: function() {
        this.animation.translateY(300).step()
        this.setData({
            animationData: this.animation.export(),
        })
    },
})